/*
 * etimer.c
 *
 *  Created on: 2023-03-13
 *      Author: chenss
 */

#include "etimer.h"
#include "tick.h"

void ETimer_Set(ETimer_t *t, uint32_t interval_ms)
{
  t->interval = (ETimer_Tick_t)interval_ms * 1000ul;
  t->start = (ETimer_Tick_t)Tick_Microseconds();
}

void ETimer_Reset(ETimer_t *t)
{
  t->start += t->interval;
}

void ETimer_Restart(ETimer_t *t)
{
  t->start = (ETimer_Tick_t)Tick_Microseconds();
}

int ETimer_Expired(ETimer_t *t)
{
  ETimer_Tick_t now = Tick_Microseconds();

  return (now >= (t->start + t->interval));
}

ETimer_Tick_t ETimer_Remaining(ETimer_t *t)
{
    ETimer_Tick_t now = (ETimer_Tick_t)Tick_Microseconds();
    ETimer_Tick_t end = t->start + t->interval;
    ETimer_Tick_t remaing = 0;

    if(now < end)
    {
        remaing = (end - now) / 1000;
        if(remaing == 0)
        {
            remaing = 1;
        }
    }


  return remaing;
}
